package com.itheima.reggie.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<Category> findCategory(Integer type) {
//        List<String> categoryList =  categoryMapper.findCategory(type);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        return categoryList;
    }

    @Override
    public Integer delete(long id) {
        //这里由于分类套餐有外键关联，所以要对另外两个表进行查询
        //根据category_id 查询dish表 是否有菜品
        Integer countDish = dishMapper.findByCid(id);
        if (countDish > 0){
            //不能删
            throw new CustomException("此分类有菜品存在，不允许删除");
        }
        //根据category_id 查询setmeal表 是否有套餐
        Integer countSetmeal = setmealMapper.findByCid(id);
        if (countSetmeal > 0){
            //不能删
            throw new CustomException("此分类有套餐存在，不允许删除");
        }
        //都没有，就可以删除了
        Integer deleteNum = categoryMapper.delete(id);
        return deleteNum;
    }

    @Override
    public Integer save(Category category) {

        return categoryMapper.save(category);
    }

    @Override
    public List<Category> findAll() {
        List<Category> categoryList = categoryMapper.findAll();
        return categoryList;
    }
}
